#!/usr/bin/env python
#regular expression for phone number of America 

import re
import sys

phonePattern = re.compile(r'''
        (\d{3}) #are code is 3 digits (e.g. 800)
        \D*     #optional separator is any character of non-digits
        (\d{3}) #trunk number is also 3 digits 
        \D*     #optional separator
        (\d{4}) #reset of number is 4 digits 
        \D*     #optional separator
        (\d*)   #extension is optional and can be any number of digits
        $       #end of string
        ''', re.VERBOSE)


def stripNumber():
    return ''.join(sys.argv[1:]).strip()


def searchNumber(number):
    phoneGroup=phonePattern.search(number).groups()
    if phoneGroup:
        if phoneGroup[3]:
            print '-'.join("%s" % d for d in phoneGroup)
        else:
            print '-'.join("%s" % d for d in phoneGroup[:3])
    else:
        return 

if __name__ == "__main__":

    '''
    Usegae:
    python phonenumber.py [number string]
    example:
    python phonenumber.py (800) 112 1234 ext 1234
    '''

    striped = stripNumber()
    if striped:
        searchNumber(striped)
